//
//  MKSystemPreviewCollectionViewCell.swift
//  MKImageBrowser3
//
//  Created by 劢克科技 on 2019/5/13.
//  Copyright © 2019 meckey. All rights reserved.
//
//  预览的Collection View Cell

import UIKit

class MKSystemPreviewCollectionViewCell: UICollectionViewCell {
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func prepareForReuse() {
        super.prepareForReuse()
        scrollView.zoomScale = 1
    }
    
    //MARK: - Setter
    
    func setImage(image: UIImage) {
        self.imageView.image = image
        
        var imageW = UIScreen.main.bounds.width
        var imageH = image.size.height * imageW / image.size.width
        var imageX: CGFloat = 0
        var imageY = (UIScreen.main.bounds.height - imageH) / 2
        
        if imageH > UIScreen.main.bounds.height {
            imageH = UIScreen.main.bounds.height
            imageW = image.size.width * imageH / image.size.height
            imageX = (UIScreen.main.bounds.width - imageW) / 2
            imageY = 0
        }
        
        scrollView.contentSize = CGSize(width: imageW, height: imageH)
        self.imageView.frame = CGRect(x: imageX, y: imageY, width: imageW, height: imageH)
    }
    
    //MARK: - Getter
    
    lazy var scrollView: UIScrollView = {
        let scroll = UIScrollView()
        scroll.minimumZoomScale = 1
        scroll.maximumZoomScale = 5
        scroll.delegate = self
        return scroll
    }()
    
    lazy var imageView: UIImageView = {
        let imageV = UIImageView()
        imageV.contentMode = .scaleAspectFill
        imageV.clipsToBounds = true
        imageV.isUserInteractionEnabled = true
        let gesture = UITapGestureRecognizer(target: self, action: #selector(toogleScroll))
        gesture.numberOfTapsRequired = 2
        imageV.addGestureRecognizer(gesture)
        return imageV
    }()
    
    //MARK: - Events
    
    @objc fileprivate func toogleScroll() {
        if scrollView.zoomScale == 1 {
            scrollView.setZoomScale(scrollView.maximumZoomScale, animated: true)
        } else {
            scrollView.setZoomScale(scrollView.minimumZoomScale, animated: true)
        }
    }
}

//MARK: - < ScrollView Delegate >

extension MKSystemPreviewCollectionViewCell: UIScrollViewDelegate {
    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return imageView
    }
    
    func scrollViewDidZoom(_ scrollView: UIScrollView) {
        let x = scrollView.bounds.width > scrollView.contentSize.width ? (scrollView.bounds.width - scrollView.contentSize.width) / 2 : 0
        let y = scrollView.bounds.height > scrollView.contentSize.height ? (scrollView.bounds.height - scrollView.contentSize.height) / 2 : 0
        imageView.center = CGPoint(x: scrollView.contentSize.width / 2 + x, y: scrollView.contentSize.height / 2 + y)
    }
}

// MARK: - UI

extension MKSystemPreviewCollectionViewCell {
    fileprivate func setupUI() {
        contentView.addSubview(scrollView)
        scrollView.addSubview(imageView)
        
        scrollView.snp.makeConstraints { (make) in
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.top.equalTo(contentView.snp_top)
            make.bottom.equalTo(contentView.snp_bottom)
        }
    }
}
